clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
foreach group in BOOK SIZE INV PROF MOME {
use portf datem cumQ* cumNRet* FE* ge* if portf==6 using $Work/portfolio_`group'_qnic, clear

foreach var of varlist ge* FE1 FE3 FE5 {
	quietly:  bys portf: egen sd=sd(`var')
	quietly: replace  `var'=`var'/sd	
	drop sd
}
tsset portf datem
gen FE=.
gen ge=ge1Mo
gen cumR=cumNRetMo1
eststo clear
eststo smp_`p'_1: quietly: ivreg2 cumQRetMo1 cumR 	if portf==6, bw(1)
eststo new_`p'_1: quietly: ivreg2 cumQRetMo1 cumR   ge 	if portf==6, bw(1)

replace FE=.
replace ge=ge1Qr
replace cumR=cumNRetQr1
eststo smp_`p'_2: quietly: ivreg2 cumQRetQr1 cumR 	if portf==6, bw(4)
eststo new_`p'_2: quietly: ivreg2 cumQRetQr1 cumR  ge 	if portf==6, bw(4)

replace FE=FE1
replace ge=ge1Yr
replace cumR=cumNRetYr1
eststo smp_`p'_3: quietly: ivreg2 cumQRetYr1 cumR 	if portf==6, bw(13)
eststo new_`p'_3: quietly: ivreg2 cumQRetYr1 cumR FE ge if portf==6, bw(13)

replace FE=FE3
replace ge=ge3Yr
replace cumR=cumNRetYr3
eststo smp_`p'_4: quietly: ivreg2 cumQRetYr3 cumR 	if portf==6, bw(37)
eststo new_`p'_4: quietly: ivreg2 cumQRetYr3 cumR FE ge	if portf==6, bw(37)

replace FE=FE5
replace ge=ge5Yr
replace cumR=cumNRetYr5
eststo smp_`p'_5: quietly: ivreg2 cumQRetYr5 cumR 	if portf==6, bw(61)
eststo new_`p'_5: quietly: ivreg2 cumQRetYr5 cumR FE ge if portf==6, bw(61)
* ---------------------------------------------------------------------------------------- *
local file "AppendixB7_June"
if "`group'"=="BOOK" {
	esttab smp* using $Tables/`file'.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title(`group') replace
}
if "`group'"!="BOOK" {
	esttab smp* using $Tables/`file'.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title(`group') append
}
	esttab new* using $Tables/`file'.csv,  nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap ar2 title(`group') append
}
